<?php
// ----------------------------------------------------策略模式----------------------------------------------
//interface PayInterface {
  //  /**
   //  * @return mixed
    // * 支付接口
   //  */
   // public function pay();
//}
/*class Alipay implements PayInterface{

  //  /**
    // * @inheritDoc
  //   */
 //   public function pay()
   // {
  //      echo  "支付宝支付".PHP_EOL;
  //  }
//}
//class WechatPay implements PayInterface{

  //  /**
  //   * @inheritDoc
 //    */
  //  public function pay()
   // {
      //  echo "微信支付".PHP_EOL;
   // }
//}
/*class Order {
    public function OrderPay(PayInterface $pay){
        $pay->pay();
    }
}
$order= new Order();
$order->OrderPay(new WechatPay());
$order->OrderPay(new Alipay());*/

// ----------------------------------------------------观察者模式-------------------------------------------
/*interface  OrderPayInterface {
    public function  PayEvent($OderId=null);
}
class OderPay implements OrderPayInterface{

    public function PayEvent($OderId = null)
    {
       echo "订单id为".$OderId."的订单状态为已支付".PHP_EOL;
    }
}*/
/*class OderYouquan implements OrderPayInterface{

    public function PayEvent($OderId = null)
    {
        echo "赠送订单id为".$OderId."的订单用户一张优惠券".PHP_EOL;
    }
}*/
//abstract class OrderEventAbstract{
    /*
     * 用来存储观察者
     */
   // private  $Observer;

    /**
     * @param OrderPayInterface $orderPayInterFace
     * 注册观察者
     */
  //  public function  register(OrderPayInterface $orderPayInterFace){
  //      $this->Observer[]=$orderPayInterFace;
  //  }
    /**
     * @param $orderId
     * 用来给观察者发送消息
     */
  //  public  function  notity($orderId){
   //     foreach ($this->Observer as $value){
   //         $value->PayEvent($orderId);
   //     }
 //   }
//}
/*class Order extends OrderEventAbstract{
    public  function pay($orderId){
        echo "支付成功".PHP_EOL;
        $this->notity($orderId);
    }
}
$order = new Order();
$order->register(new OderPay());
$order->register(new OderYouquan());
$order->pay(1);*/
// ----------------------------------------------------命令链模式-------------------------------------------
/*
 * 权限接口
 */
/*interface RoleLoginInterface {
    public function hanlde($name);
}
class  bronze implements RoleLoginInterface{

    public function hanlde($name)
    {
        echo "欢迎".$name."进入直播间".PHP_EOL;
    }
}
class  sliver implements  RoleLoginInterface{

    public function hanlde($name)
    {
        echo "掌声欢迎白银会员".$name."进入直播间".PHP_EOL;
    }
}
class  gold implements  RoleLoginInterface{
    public function hanlde($name)
    {
        echo "掌声欢迎黄金会员".$name."进入直播间，鞭炮，掌声齐鸣".PHP_EOL;
    }
}*/
//class  Role {

  //  private  $role=[];
   /*
    * 注册不用等级的角色到不用的角色类目
    */
/*    public function  register(array $login){
        $this->role=$login;
    }
  public  function  LoginRequest($name,$role){
        foreach ($this->role as $key=>$value){
            if ($key==$role){
                $value->hanlde($name);
            }
        }
  }
}
$role = new Role();
$role->register(array(
    'bronze'=>new bronze(),
    'sliver' => new sliver(),
    'gold'=>new gold()
));

$role->LoginRequest("张三","sliver");*/
// ----------------------------------------------------迭代器模式-------------------------------------------
class CycleIterator implements Iterator{
    private $array=[];
    private $currenIndex;
    public function __construct($array)
    {
        $this->array=$array;
    }

    /**
     * @inheritDoc
     */
    public function current()
    {
       return $this->array[$this->currenIndex];
    }

    /**
     * @inheritDoc
     */
    public function next()
    {
           ++$this->currenIndex;
    }

    /**
     * @inheritDoc
     */
    public function key()
    {
    return $this->currenIndex;
    }

    /**
     * @inheritDoc
     */
    public function valid()
    {
      return isset($this->array[$this->currenIndex]);
    }

    /**
     * @inheritDoc
     */
    public function rewind()
    {
       $this->currenIndex=0;
    }

    /**
     * @return mixed
     */
    public function getArray()
    {
        return $this->array;
    }

    /**
     * @return mixed
     */
    public function getCurrenIndex()
    {
        return $this->currenIndex;
    }

    /**
     * @param mixed $array
     */
    public function setArray($array)
    {
        $this->array = $array;
    }

    /**
     * @param mixed $currenIndex
     */
    public function setCurrenIndex($currenIndex)
    {
        $this->currenIndex = $currenIndex;
    }
}
$list=[1,2,3,4,5,6,7,8,9,0];
$cycle = new CycleIterator($list);
foreach ($cycle as $value){
   echo $value.PHP_EOL;
}